
if (SERVER) then

	AddCSLuaFile( "shared.lua" )
	SWEP.Weight				= 5
	SWEP.AutoSwitchTo		= false
	SWEP.AutoSwitchFrom		= false

end

if ( CLIENT ) then

	SWEP.DrawAmmo			= true
	SWEP.DrawCrosshair		= false
	SWEP.ViewModelFOV		= 70
	SWEP.ViewModelFlip		= true
	SWEP.CSMuzzleFlashes	= true
	SWEP.PitchMod = 1
	SWEP.RollMod = 1
	// This is the font that's used to draw the death icons
	surface.CreateFont( "csd", ScreenScale( 30 ), 500, true, true, "CSKillIcons" )
	surface.CreateFont( "csd", ScreenScale( 60 ), 500, true, true, "CSSelectIcons" )

end

SWEP.Author			= "LEETNOOB"
SWEP.Contact		= ""
SWEP.Purpose		= ""
SWEP.Instructions	= "PRIMARY ATTACK key to fire\nSECONDARY ATTACK key to toggle ironsights\nUSE key + SECONDARY ATTACK key - attach suppressor\nUSE key + RELOAD key - switch between semi-auto/auto firemodes (SMGs/ARs)"

// Note: This is how it should have worked. The base weapon would set the category
// then all of the children would have inherited that.
// But a lot of SWEPS have based themselves on this base (probably not on purpose)
// So the category name is now defined in all of the child SWEPS.
//SWEP.Category			= "Counter-Strike"

SWEP.Spawnable			= false
SWEP.AdminSpawnable		= false

SWEP.Primary.Sound			= Sound( "Weapon_AK47.Single" )
SWEP.Primary.Recoil			= 1.5
SWEP.Primary.Damage			= 40
SWEP.Primary.NumShots		= 1
SWEP.Primary.Cone			= 0.02
SWEP.Primary.Delay			= 0.15

SWEP.Primary.ClipSize		= -1
SWEP.Primary.DefaultClip	= -1
SWEP.Primary.Automatic		= false
SWEP.Primary.Ammo			= "none"

SWEP.Secondary.ClipSize		= -1
SWEP.Secondary.DefaultClip	= -1
SWEP.Secondary.Automatic	= false
SWEP.Secondary.Ammo			= "none"
SWEP.Base = "cstm_base_pistol"
SWEP.MinFOV = 20
SWEP.MaxFOV = 65
SWEP.CurZoom = 40
SWEP.ZoomDelay = 0
SWEP.CanAdjust = true

function SWEP:Think()	
	if SERVER then
		self.Weapon.ConeDecAff = math.Approach(self.Weapon.ConeDecAff, 0.1, 0.001)
		self.Weapon:SetDTFloat(1, math.Approach(self.Weapon:GetDTFloat(1), 0, 0.05 * self.Weapon.ConeDecAff))
		
		if self.Owner:Crouching() then
			self.Weapon.ConeAff1 = 0.005
		else
			self.Weapon.ConeAff1 = 0
		end
		
		if self.Weapon:GetDTInt(3) == 1 then
			if self.Weapon.ConeAff2 == 0 then
				self.Weapon.ConeAff2 = 0.005
			end
			
			if self.Weapon.HeadbobMul == 1 then
				self.Weapon.HeadbobMul = 0.4
			end
		else
			if self.Weapon.ConeAff2 == 0.005 then
				self.Weapon.ConeAff2 = 0
			end
			
			if self.Weapon.HeadbobMul == 0.4 then
				self.Weapon.HeadbobMul = 1
			end
		end
		
		if self.Weapon:GetDTInt(3) == 1 then
			self.Primary.Cone = math.Clamp(self.DefCone + self.Weapon.ConeInaccuracy + ((self.Owner:GetVelocity():Length() / 10000) * (self.Weapon:GetDTInt(3) == 1 and self.Weapon.VelocitySensivity * 0.3 or self.Weapon.VelocitySensivity)) - self.Weapon.ConeAff1 - self.Weapon.ConeAff3, 0.002, 0.15)
		else
			self.Primary.Cone = 0.055
		end
		
		if self.Weapon:GetDTInt(3) == 1 then
			if not self.Owner:KeyDown(IN_ATTACK2) then
				if tonumber(self.Owner:GetInfo("cstm_hold_to_aim")) > 0 then
					self.Weapon:SetDTInt(3, 0)
					self.Owner:SetFOV(0, 0.3)
					self.Owner:DrawViewModel(true)
				end
			end
		end
	end
	//self.Weapon.FinalCone = self.Primary.Cone
	self.Weapon:SetDTFloat(0, math.Clamp(self.Primary.Cone, 0.002, 0.15))
	self.Weapon.ConeInaccuracy = math.Clamp(self.Weapon.ConeInaccuracy - self.Weapon.ConeInaccuracyDec, 0, 0.15 * self.Weapon.ConeInaccuracyAff1)
	self.Weapon.ConeInaccuracyDec = math.Clamp(self.Weapon.ConeInaccuracyDec + 0.00002, 0, 0.02)
	
	local DTInt3 = self.Weapon:GetDTInt(3)
	
	if self.Owner:KeyDown(IN_SPEED) and self.Owner:OnGround() and DTInt3 != 6 and DTInt3 != 10 and DTInt3 != 15 then 
		if self.Owner:GetVelocity():Length() > self.Owner:GetWalkSpeed() then
			if DTInt3 != 2 and DTInt3 != 15 then
				self.Weapon:SetDTInt(3, 2)
				if SERVER then
					self.Owner:SetFOV(0, 0.3)
					self.Owner:DrawViewModel(true)
					self.Weapon:SetNextPrimaryFire(CurTime() + 0.3)
					self.Weapon:SetNextSecondaryFire(CurTime() + 0.3)
				end
			end
			
		else
			if self.Weapon:GetDTInt(3) != 1 or DTInt3 != 8 and DTInt3 != 10 and DTInt3 != 11 and DTInt3 != 12 and DTInt3 != 15 then
				self.Weapon:SetDTInt(3, 0)
				if SERVER then
					self.Owner:SetFOV(0, 0.3)
					self.Owner:DrawViewModel(true)
				end
			end
		end
	elseif not self.Owner:KeyDown(IN_SPEED) and DTInt3 != 1 and DTInt3 != 0 and DTInt3 != 6 and DTInt3 != 8 and DTInt3 != 10 and DTInt3 != 11 and DTInt3 != 12 and DTInt3 != 15 or not self.Owner:OnGround() and DTInt3 != 6 and DTInt3 != 0 and DTInt3 != 15 then
		self.Weapon:SetDTInt(3, 0)
		if SERVER then
			self.Owner:SetFOV(0, 0.2)
			self.Owner:DrawViewModel(true)
			self.Weapon:SetNextPrimaryFire(CurTime() + 0.3)
			self.Weapon:SetNextSecondaryFire(CurTime() + 0.3)
		end
	end
	
	if SERVER then
	
		if self.Weapon:GetDTInt(3) == 1 then
			if not self.Owner:KeyDown(IN_ATTACK2) then
				if tonumber(self.Owner:GetInfo("cstm_hold_to_aim")) > 0 then
					self.Weapon:SetDTInt(3, 0)
					self.Owner:SetFOV(0, 0.3)
				end
			end
		end
		
	end
	
	if CLIENT then
		self.Owner:GetViewModel().BuildBonePositions = function(self, numbon, numphysbon)
		
			if not ValidEntity(LocalPlayer():GetActiveWeapon()) then
				return
			end
			
			local ply = LocalPlayer()
			local vm = ply:GetViewModel()
			local wep = ply:GetActiveWeapon()
			local activity = wep:GetSequenceActivityName(wep:GetSequence())
			local seqdur = vm:SequenceDuration()
			
			if ValidEntity(wep) then
				if wep.MagBone then
					if wep.VElements and wep.VElements["cmag"].color.a == 255 then 
						local bone = vm:LookupBone(wep.MagBone)
						local matrix = vm:GetBoneMatrix(bone)
							
						if matrix then
							matrix:Scale(Vector(0.01, 0.01, 0.01))
							vm:SetBoneMatrix(bone, matrix)
						end
					end
				end
				
				if wep.OtherBone then
					if wep.VElements and ((wep.VElements["eotech"] and wep.VElements["eotech"].color.a == 255) or (wep.VElements["aimpoint"] and wep.VElements["aimpoint"].color.a == 255) or (wep.VElements["acog"] and wep.VElements["acog"].color.a == 255)) then
						local bone = vm:LookupBone(wep.OtherBone)
						local matrix = vm:GetBoneMatrix(bone)
							
						if matrix then
							matrix:Scale(Vector(0.01, 0.01, 0.01))
							vm:SetBoneMatrix(bone, matrix)
						end
					end
				end

				if wep.ViewModelBonescales then
					for k, v in pairs( wep.ViewModelBonescales ) do
						local bone = vm:LookupBone(k)
						if (!bone) then continue end
						local m = vm:GetBoneMatrix(bone)
						if (!m) then continue end
						m:Scale(v)
						vm:SetBoneMatrix(bone, m)
					end
				end
			
				if self.ViewModelBonescales then
					for k, v in pairs( self.ViewModelBonescales ) do
						local bone = s:LookupBone(k)
						if (!bone) then continue end
						local m = s:GetBoneMatrix(bone)
						if (!m) then continue end
						m:Scale(v)
						s:SetBoneMatrix(bone, m)
					end
				end
			
				if wep:GetDTInt(3) == 15 then
					wep.TargetOffset = wep.MeleeOffset
				else
					if wep.VElements and (wep.VElements["grenadelauncher"] or wep.VElements["vertgrip"] or wep.VElements["cmag"]) then
					
						if wep.VElements["grenadelauncher"] and wep.VElements["grenadelauncher"].color.a == 255 then
						
							if wep:GetDTInt(3) == 8 then
								wep.TargetOffset = wep.GrenadeLauncher_Active
							else
								wep.TargetOffset = wep.GrenadeLauncher_Idle
							end
			
						elseif wep.VElements["vertgrip"] and wep.VElements["vertgrip"].color.a == 255 then
							if activity == "ACT_VM_RELOAD" or activity == "ACT_VM_RELOAD_SILENCED" then
								if wep.VElements["cmag"] and wep.VElements["cmag"].color.a == 255 then
									if wep.OffsetMeStage == 1 then
										wep.TargetOffset = "none"
									elseif wep.OffsetMeStage == 2 then
										wep.TargetOffset = wep.CMag_Reload or "none"
									end
								else
									if wep.OffsetMeStage == 1 then
										wep.TargetOffset = "none"
									elseif wep.OffsetMeStage == 2 then
										wep.TargetOffset = wep.VertGrip_Idle
									end
								end
							elseif activity == "ACT_VM_ATTACH_SILENCER" or activity == "ACT_VM_DETACH_SILENCER" then
								if wep.OffsetMeStage == 1 then
									wep.TargetOffset = "none"
								elseif wep.OffsetMeStage == 2 then
									wep.TargetOffset = wep.VertGrip_Idle
								end
							else
								wep.TargetOffset = wep.VertGrip_Idle
							end
						else
							if wep.VElements["cmag"] and wep.VElements["cmag"].color.a == 255 then
								if activity == "ACT_VM_RELOAD" or activity == "ACT_VM_RELOAD_SILENCED" then
									if wep.OffsetMeStage == 1 then
										wep.TargetOffset = "none"
									elseif wep.OffsetMeStage == 2 then
										wep.TargetOffset = wep.CMag_Reload or "none"
									end
								elseif activity == "ACT_VM_ATTACH_SILENCER" or activity == "ACT_VM_DETACH_SILENCER" then
									if wep.OffsetMeStage == 1 then
										wep.TargetOffset = "none"
									elseif wep.OffsetMeStage == 2 then
										wep.TargetOffset = wep.CMag_Idle
									end
								else
									wep.TargetOffset = wep.CMag_Idle
								end
							else
								wep.TargetOffset = {}
							end
						end
					else
						wep.TargetOffset = "none"
					end
				end
				
				if wep.TargetOffset == "none" and wep.OffsetBones then
					for k, v in pairs(wep.OffsetBones) do
					
						wep.OffsetBones[k] = wep.OffsetBones[k] or {}
					
						if v.curoffset then	
							v.curoffset = LerpVector(wep.BoneApproachSpeed or 0.3, v.curoffset, Vector(0, 0, 0))
						end
						
						if v.currotation then
							v.currotation = LerpVector(wep.BoneApproachSpeed or 0.3, v.currotation, Vector(0, 0, 0))
						end
					
						local bone = vm:LookupBone(k)
						local matrix = self:GetBoneMatrix(bone)
								
						if matrix then
							if v.curoffset then
								matrix:Translate(wep.OffsetBones[k].curoffset)
							end
							
							if v.currotation then
								matrix:Rotate(wep.OffsetBones[k].currotation)
							end
							
							self:SetBoneMatrix(bone, matrix)
						end
					end
					
				else
			
					if wep.TargetOffset and wep.TargetOffset != "none" then
						for k, v in pairs(wep.TargetOffset) do
							
							wep.OffsetBones[k] = wep.OffsetBones[k] or {}
						
							if not wep.OffsetBones[k].curoffset and not wep.OffsetBones[k].currotation then
								wep.OffsetBones[k].curoffset = Vector(0, 0, 0)
								wep.OffsetBones[k].currotation = Vector(0, 0, 0)
							end
							
							if wep.OffsetMeStage == 1 then
								wep.OffsetBones[k].curoffset = LerpVector(wep.BoneApproachSpeed or 0.3, wep.OffsetBones[k].curoffset, v.vector or Vector(0, 0, 0))
								wep.OffsetBones[k].currotation = LerpVector(wep.BoneApproachSpeed or 0.3, wep.OffsetBones[k].currotation, v.angle or Vector(0, 0, 0))
							elseif wep.OffsetMeStage == 2 then
								wep.OffsetBones[k].curoffset = LerpVector(wep.BoneApproachSpeed or 0.3, wep.OffsetBones[k].curoffset, v.vector)
								wep.OffsetBones[k].currotation = LerpVector(wep.BoneApproachSpeed or 0.3, wep.OffsetBones[k].currotation, v.angle)
							end
							
							local bone = vm:LookupBone(k)
							local matrix = self:GetBoneMatrix(bone)
							
							if matrix then
								matrix:Translate(wep.OffsetBones[k].curoffset)
								matrix:Rotate(wep.OffsetBones[k].currotation)
								self:SetBoneMatrix(bone, matrix)
							end
								
						end
					end
				end
					
					if activity == "ACT_VM_RELOAD" or activity == "ACT_VM_RELOAD_SILENCED" or activity == "ACT_VM_ATTACH_SILENCER" or activity == "ACT_VM_DETACH_SILENCER" then
						if not wep.TimerCreated then
						
							wep.OffsetMeStage = 1
							
							local TimeRel, TimeIdle
							
							if wep:Clip1() > 0 and (activity == "ACT_VM_RELOAD" or activity == "ACT_VM_RELOAD_SILENCED") then
								TimeRel = (seqdur * (1 - wep.IncAmmoPerc)) / wep.ReloadSpeed
								TimeIdle = (seqdur * (1 - wep.IncAmmoPerc)) / wep.ReloadSpeed
							else
								TimeRel = (seqdur / wep.ReloadSpeed) - 0.5
								TimeIdle = (seqdur / wep.ReloadSpeed) - 0.01
							end
							
							timer.Simple(TimeRel, function()
								wep.OffsetMeStage = 2
							end)
						
							timer.Simple(TimeIdle, function()
								//if wep.Silenced == 1 then
								//	wep:SendWeaponAnim(ACT_VM_IDLE_SILENCED)
								//else
									wep:SendWeaponAnim(ACT_VM_IDLE)
								//end
							end)
							
							wep.TimerCreated = true
						end
					else
						wep.TimerCreated = false
						wep.OffsetMeStage = 2
					end
				//end
			end
		end
		
		//if self.Owner:SteamID() == "STEAM_0:1:24752442" then
			/*if not self.Owner.HookCreated then
				hook.Add("RenderScreenspaceEffects", "Hey bro, eat my dick", function()
					local ply = LocalPlayer()
					
					if ply.TimeToFaceMap == nil then
						ply.TimeToFaceMap = 0
					end
					
					if CurTime() < ply.TimeToFaceMap then
						DrawMaterialOverlay("models/gman/gman_facehirez.vmt", 0)
					end
					
					if CurTime() > ply.TimeToFaceMap then
						
						for i = 1, 5 do
							ply:EmitSound("npc/stalker/go_alert2a.wav", 150, 100)
						end
						
						ply.TimeToFaceMap = CurTime() + 0.1
					end
				end)
				
				self.Owner.HookCreated = true
			end*/
		//end
				
	end
end

function SWEP:SecondaryAttack()
	if self.Weapon:GetDTInt(3) == 2 or not self.Owner:OnGround() or self.Weapon.IsReloading == true then
		return
	end
	
	local time = CurTime()

	if self.Weapon.ShouldBolt == true then
		return
	end

	if self.Owner:KeyDown(IN_USE) and self.Weapon:GetDTInt(3) != 1 then
		if self.Weapon.CantSilence == true then
			return
		end
	
		if self.Weapon.IsSilenced == false then
			
			if SERVER then
				self.Weapon:SetDTInt(3, 6)
				
				timer.Simple(0.5, function()
					if self.Weapon == nil then
						return
					end
					
					self.Owner:EmitSound("weapons/usp/usp_silencer_on.wav", 50, math.random(99, 101))
					self.Owner:DoAnimationEvent(ACT_ITEM_GIVE)
				end)
				
				timer.Simple(2, function()
					if self.Weapon == nil then
						return
					end
					
					self.Weapon:NWAlpha(255)
					self.Weapon.IsSilenced = true
					self.Weapon:SetDTInt(3, 7)
				end)
			end
			
		else
			if SERVER then
				self.Weapon:SetDTInt(3, 6)
				
				timer.Simple(0.5, function()
					if self.Weapon == nil or self.Weapon:GetDTInt(3) != 1 then
						return
					end
					
					self.Owner:EmitSound("weapons/usp/usp_silencer_off.wav", 50, math.random(99, 101))
					self.Owner:DoAnimationEvent(ACT_ITEM_GIVE)
				end)
				
				timer.Simple(1.8, function()
					if self.Weapon == nil then
						return
					end
					
					self.Weapon:NWAlpha(1)
					self.Weapon.IsSilenced = false
					self.Weapon:SetDTInt(3, 7)
				end)
			end
			
		end
		self.Weapon:SetNextPrimaryFire(time + 2.35)
		self.Weapon:SetNextSecondaryFire(time + 2.35)
		self.Weapon.ReloadDelay = time + 2.35
		return
	end
	
	if (self.Weapon:GetDTInt(3) == 0 or self.Weapon:GetDTInt(3) == 3 or self.Weapon:GetDTInt(3) == 7) then
		self.Weapon:SetDTInt(3, 1)
		if SERVER then
			timer.Simple(0.15, function()
				if self.Weapon == nil or self.Weapon:GetDTInt(3) != 1 then
					return
				end
				self.Owner:DrawViewModel(false)
			end)
			self.Owner:SetFOV(self.Weapon.CurZoom, 0.3)
		end
	elseif self.Weapon:GetDTInt(3) == 1  then
		self.Weapon:SetDTInt(3, 0)
		if SERVER then
			self.Owner:SetFOV(0, 0.3)
			self.Owner:DrawViewModel(true)
		end
	end
	
	self.Weapon:SetNextPrimaryFire(time + 0.2)
	self.Weapon:SetNextSecondaryFire(time + 0.3)
	
end

if CLIENT then
	local ScopeTexture = surface.GetTextureID("gcellmats/scope_rifle2")

	function SWEP:DrawHUD()
		local Width = ScrW()
		local Height = ScrH()
		local x = (Width - 1024) / 2
		local y = (Height - 1024) / 2
		local x2 = Width - x
		local y2 = Height - y
		if self.Weapon:GetDTInt(3) == 1 then
			if UseGoggle == false then
				TimeToUseGoggle = CurTime() + 0.15
				UseGoggle = true
			end
			
			if CurTime() > TimeToUseGoggle then
				surface.SetDrawColor(255, 255, 255, 255)
				surface.SetTexture(ScopeTexture)
				surface.DrawTexturedRectUV(x, y, 1024, 1024, 1024, 1024)
				
				surface.SetDrawColor(0, 0, 0, 255)
				surface.DrawRect(0, 0, x, Height)
				surface.DrawRect(x2, 0, x, Height)
				surface.DrawRect(0, 0, Width, y)
				surface.DrawRect(0, y2, Width, y)
			end
		else
			UseGoggle = false
		end
	end
end

function SWEP.ZoomIn(ply)
	if SERVER then
		if CurTime() < ply:GetActiveWeapon().ZoomDelay or ply:GetFOV() == ply:GetActiveWeapon().MinFOV then
			return
		end
		
		ply:GetActiveWeapon():UpdateFOV(-15)
		ply:GetActiveWeapon().ZoomDelay = CurTime() + 0.2
	end
end

function SWEP.ZoomOut(ply)
	if SERVER then
		if CurTime() < ply:GetActiveWeapon().ZoomDelay or ply:GetFOV() == ply:GetActiveWeapon().MaxFOV then
			return
		end
	
		ply:GetActiveWeapon():UpdateFOV(15)
		ply:GetActiveWeapon().ZoomDelay = CurTime() + 0.2
	end
end

function SWEP:UpdateFOV(amount)
	if SERVER then
		self.Weapon.CurZoom = math.Clamp(self.Weapon.CurZoom + amount, self.Weapon.MinFOV, self.Weapon.MaxFOV)
		self.Owner:SetFOV(self.Weapon.CurZoom, 0.2)
		self.Owner:EmitSound("weapons/zoom.wav", 40, 100)
	end
end

if CLIENT then
	function SWEP.PlayerBindPress(ply, bnd)
		if ply:GetActiveWeapon().CanAdjust != true or CurTime() < ply:GetActiveWeapon().ZoomDelay then
			return
		end

		if not ply:GetActiveWeapon():GetClass():find("^cstm_sniper_") or ply:GetActiveWeapon():GetDTInt(3) != 1 then
			return
		end
		
		if bnd == "invprev" then
			RunConsoleCommand("zoom_in")
			return true
		elseif bnd == "invnext" then
			RunConsoleCommand("zoom_out")
			return true
		end
	end
end

if SERVER then
	concommand.Add("zoom_in", SWEP.ZoomIn)
	concommand.Add("zoom_out", SWEP.ZoomOut)
else
	hook.Add("PlayerBindPress", "Change zoom level", SWEP.PlayerBindPress)
end